<?php if(!defined('SIDELOADED')) { echo 'Direct access is not allowed.'; }
/**
 * @author Tyil <info@tyil.nl>
 * @version 1.0 beta [30-03-2012]
 */

class Website {
  
  /**
   * Load a view.
   * @param string $view The name of the view.
   * @param array $variables An array filled with variables.
   * @return mixed Returns false on 404, or returns the parsed view. 
   */
  public static function View($view, $variables = null, $echo = false) {
    
    if(file_exists(PATH . '/content/view/' . $view . '.php')) {
      
      ob_start();
      
      // Parse variables along
      if(@$variables) { foreach($variables as $key => $var) { $$key = $var; } }
      
      // Include the view
      include PATH . '/content/view/' . $view . '.php';
      
      // And return the view
      if($echo) {
        
        echo ob_get_clean();
        
      } else {
        
        return ob_get_clean();
        
      }
      
    } else {
      
      user_error('The view "' . $view . '" could not be loaded (404).', E_USER_WARNING);
      
      return false;
      
    }
    
  }
  
  /**
   * Load a given class.
   * @param string $class The class you wish to load.
   */
  public static function LoadClass($class) {
    
    if(file_exists(PATH . '/custom/class/' . $class . '.php')) {
      
      include PATH . '/custom/class/' . $class . '.php';
      
    } elseif(file_exists(PATH . '/core/class/' . $class . '.php')) {
      
      include PATH . '/core/class/' . $class . '.php';
      
    } else {
      
      user_error('Could not load class "' . $class . '".', E_USER_WARNING);
      
    }
    
  }
  
  public static function LoadFunction($function) {
    
    if(file_exists(PATH . '/custom/function/' . $function . '/')) {
      // There's a directory of functions
      
      $files = scandir(PATH . '/custom/function/' . $function . '/');
      
      foreach($files as $file) {
        
        if(!in_array($file, array('.', '..', '.svn', '.htaccess'))) {
        
          include PATH . '/custom/function/' . $function . '/' . $file;
        
        }
        
      }
      
    } elseif(file_exists(PATH . '/custom/function/' . $function . '.php')) {
      
      include PATH . '/custom/function/' . $function . '.php';
      
    } elseif(file_exists(PATH . '/core/function/' . $function . '/')) {
      // There's a directory of core functions
      
      $files = scandir(PATH . '/core/function/' . $function . '/');
      
      foreach($files as $file) {
        
        if(!in_array($file, array('.', '..', '.svn', '.htaccess'))) {
        
          include PATH . '/core/function/' . $function . '/' . $file;
        
        }
        
      }
      
    } elseif(file_exists(PATH . '/core/function/' . $function . '.php')) {
      
      include PATH . '/core/function/' . $function . '.php';
      
    } else {
      
      user_error('Could not load function "' . $function . '"', E_USER_WARNING);
      
    }
    
  }
  
}
